/*
* $Id: ChangeGLWnd.cpp 141 2010-09-04 09:36:13Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Alexey Volokitin
* All rights reserved
*
*/
#include "Common/include.h"
#include "ChangeGLWnd.h"
#include "Components/Journal/QueenBeeManager.h"
#include "Components/Journal/JournalInterface.h"
#include "Database/Tables/FamiliesTable.h"
#include "../BQManagerDialog.h"

CChangeGLWnd::CChangeGLWnd(type tp)
: m_type(tp){
	m_pBrush.CreateSolidBrush(RGB(255, 255, 255));
}
CChangeGLWnd::~CChangeGLWnd(){
	m_pBrush.DeleteObject();
}

void CChangeGLWnd::WindowCreate(CWnd* pParent){
	Create(AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, LoadCursor(NULL, IDC_ARROW), m_pBrush),
		_T(""), WS_CHILD | WS_VISIBLE | WS_BORDER, RECT_LEFT_WIN, pParent, LEFT_WIN_ID);
}

void CChangeGLWnd::WindowDestroy(){
	DestroyWindow();
}

void CChangeGLWnd::SetQueenBeeId(int nId){
	__super::SetQueenBeeId(nId);
	CQueenBeeManager::queen_bee qb = IJournalComponent::GetJournalComponent()->GetQueenBeeManager()->GetQueenBee(nId);
	m_strNameQB = qb.s_strName;
	if(qb.s_nIdParentQueenBee)
		m_strNameParentQB = IJournalComponent::GetJournalComponent()->GetQueenBeeManager()->GetQueenBee(qb.s_nIdParentQueenBee).s_strName;
	else
		m_strNameParentQB = IApplication::GetApp()->GetLanguageManager()->GetString(NO_CURRENT_PARENT_QB_ID);
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CFamiliesTable table;
	table.m_content.s_nId = qb.s_nIdFamily;
	IResult* pResult = table.GetValue(pDb);
	if(pResult->GetSizeResult())
		m_strNameFamily = ((CFamiliesTable::content*)(*pResult)[0])->s_strName;
	else
		m_strNameFamily = IApplication::GetApp()->GetLanguageManager()->GetString(NO_CURRENT_FAMILY_QB_ID);
	pResult->Free();
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
}

void CChangeGLWnd::SetSelectionData(unsigned long data){
	__super::SetSelectionData(data);
	m_nIdFamily = 0;
	if(LOWORD(m_selData) == CBQManagerDialog::FAMILY){
		m_nIdFamily = HIWORD(m_selData);
		m_strNameParent = IApplication::GetApp()->GetLanguageManager()->GetString(NO_PARENT_QB_ID);
		m_nIdQueenBee = 0;
	}else if(LOWORD(m_selData) == CBQManagerDialog::QUEEN_BEE){
		CQueenBeeManager::queen_bee qb = IJournalComponent::GetJournalComponent()->GetQueenBeeManager()->GetQueenBee(HIWORD(m_selData));
		m_nIdFamily = qb.s_nIdFamily;
		m_strNameParent = qb.s_strName;
		m_nIdQueenBee = HIWORD(m_selData);
	}else{
		m_strNameParent = IApplication::GetApp()->GetLanguageManager()->GetString(NO_PARENT_QB_ID);
		m_nIdQueenBee = 0;
	}
	if(m_nIdFamily){
		IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
		CFamiliesTable table;
		table.m_content.s_nId = m_nIdFamily;
		IResult* pResult = table.GetValue(pDb);
		m_strNewFamily = ((CFamiliesTable::content*)(*pResult)[0])->s_strName;
		pResult->Free();
		IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
	}else
		m_strNewFamily = IApplication::GetApp()->GetLanguageManager()->GetString(NO_FAMILY_QB_ID);
	Invalidate();
}

void CChangeGLWnd::Save(){
	if(m_type == FAMILY)
		IJournalComponent::GetJournalComponent()->GetQueenBeeManager()->ChangeFamilyQB(m_nId, m_nIdFamily);
	if(m_type == PARENT)
		IJournalComponent::GetJournalComponent()->GetQueenBeeManager()->ChangeParentQB(m_nId, m_nIdQueenBee);
}

BEGIN_MESSAGE_MAP(CChangeGLWnd, CWnd)
	ON_WM_PAINT()
END_MESSAGE_MAP()

afx_msg void CChangeGLWnd::OnPaint(){
	CPaintDC dc(this);
	dc.SetBkMode(TRANSPARENT);
	CFont* pFont = CCommonFunction::GetFont(_T("Arial"), 20, TRUE, TRUE, FALSE);
	HGDIOBJ oldFont = dc.SelectObject(pFont->m_hObject);
	CRect rect;
	GetClientRect(rect);
	std::tstring strHeader;
	if(m_type == FAMILY)
		strHeader = IApplication::GetApp()->GetLanguageManager()->GetString(CHANGE_QB_FAMILY_ID) + _T(" - ") + m_strNameQB;
	if(m_type == PARENT)
		strHeader = IApplication::GetApp()->GetLanguageManager()->GetString(CHANGE_QB_PARENT_ID) + _T(" - ") + m_strNameQB;
	dc.DrawText(strHeader.c_str(), rect, DT_CENTER);
	dc.SelectObject(oldFont);
	pFont->DeleteObject();
	delete pFont;
	
	std::tstring strCurString, strNewString, strCurName, strNewName;
	if(m_type == FAMILY){
		strCurString = IApplication::GetApp()->GetLanguageManager()->GetString(CURRENT_FAMILY_QB_ID);
		strNewString = IApplication::GetApp()->GetLanguageManager()->GetString(NEW_FAMILY_QB_ID);
		strCurName = m_strNameFamily;
		strNewName = m_strNewFamily;
	}else if(m_type == PARENT){
		strCurString = IApplication::GetApp()->GetLanguageManager()->GetString(CURRENT_PARENT_QB_ID);
		strNewString = IApplication::GetApp()->GetLanguageManager()->GetString(NEW_PARENT_QB_ID);
		strCurName = m_strNameParentQB;
		strNewName = m_strNameParent;
	}
	dc.DrawText(strCurString.c_str(), CRect(rect.left, 25, rect.right, 45), DT_LEFT | DT_SINGLELINE | DT_VCENTER);
	dc.DrawText(strNewString.c_str(), CRect(rect.left, 70, rect.right, 90), DT_LEFT | DT_SINGLELINE | DT_VCENTER);
	pFont = CCommonFunction::GetFont(_T("Arial"), 16, FALSE, TRUE, FALSE);
	oldFont = dc.SelectObject(pFont->m_hObject);
	dc.DrawText(strCurName.c_str(), CRect(rect.left, 43, rect.right, 63), DT_CENTER | DT_SINGLELINE | DT_VCENTER);
	dc.DrawText(strNewName.c_str(), CRect(rect.left, 93, rect.right, 113), DT_CENTER | DT_SINGLELINE | DT_VCENTER);
	dc.SelectObject(oldFont);
	pFont->DeleteObject();
	delete pFont;
}
